package second.stackandqueue;

import java.util.LinkedList;
import java.util.List;

public class Solution06 {
    public int evalRPN(String[] tokens) {
        List<Integer> stack = new LinkedList<>();
        for (int i = 0; i < tokens.length; i++) {
            if ("+".equals(tokens[i])) {
                stack.add(stack.remove(stack.size() - 1) + stack.remove(stack.size() - 1));
            } else if ("*".equals(tokens[i])) {
                stack.add(stack.remove(stack.size() - 1) * stack.remove(stack.size() - 1));
            } else if ("-".equals(tokens[i])) {
                stack.add(-stack.remove(stack.size() - 1) + stack.remove(stack.size() - 1));
            } else if ("/".equals(tokens[i])) {
                Integer i1 = stack.remove(stack.size() - 1);
                Integer i2 = stack.remove(stack.size() - 1);
                stack.add(i2 / i1);
            } else {
                stack.add(Integer.parseInt(tokens[i]));
            }
        }
        return stack.get(0);
    }
}
